﻿#ifndef MYGRAPHICSVIEW_H
#define MYGRAPHICSVIEW_H


#include <QGraphicsView>
#include <QGraphicsScene>
#include <QWheelEvent>

class MyGraphicsView : public QGraphicsView {
public:
    MyGraphicsView(QGraphicsScene* scene, QWidget* parent = nullptr)
        : QGraphicsView(scene, parent), m_ctrlPressed(false) {
        setRenderHint(QPainter::Antialiasing); // 设置抗锯齿渲染
    }

protected:
    void wheelEvent(QWheelEvent* event) override {
        // 检查Ctrl键是否被按下
        if (m_ctrlPressed) {
            qreal scaleFactor = 1.15;
            if (event->delta() > 0) {
                // 向上滚动，放大视图
                scale(scaleFactor, scaleFactor);
            } else {
                // 向下滚动，缩小视图
                scale(1.0 / scaleFactor, 1.0 / scaleFactor);
            }
        } else {
            // 如果Ctrl键未被按下，则使用默认的滚轮事件处理程序
            QGraphicsView::wheelEvent(event);
        }
    }

    void keyPressEvent(QKeyEvent* event) override {
        // 按下Ctrl键
        if (event->key() == Qt::Key_Control) {
            m_ctrlPressed = true;
        }
        // 将事件传递给基类处理
        QGraphicsView::keyPressEvent(event);
    }

    void keyReleaseEvent(QKeyEvent* event) override {
        // 释放Ctrl键
        if (event->key() == Qt::Key_Control) {
            m_ctrlPressed = false;
        }
        // 将事件传递给基类处理
        QGraphicsView::keyReleaseEvent(event);
    }

private:
    bool m_ctrlPressed; // 记录Ctrl键的状态
};
#endif // MYGRAPHICSVIEW_H
